package edu.spec.sys.util;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;

public class FileTool {

	public static String saveTempFile(MultipartFile file, HttpServletRequest req){
		String tempFullPath = req.getSession().getServletContext().getRealPath("/") + "\\resources\\temp\\";
		try {
			String orgName = file.getOriginalFilename();
			String extension = FilenameUtils.getExtension(orgName);
			String storeName = String.valueOf(new Date().getTime()) + "." + extension;
			String fullStorePath = tempFullPath + storeName;
			FileUtils.copyInputStreamToFile(file.getInputStream(), new File(fullStorePath));
			return fullStorePath;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return "";
	}
}
